//
//  ZGLTopicTypeVC.m
//  Faker
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLTopicTypeVC.h"
#import "ZGLTopicImageTableViewCell.h"
#import "ZGLTopicWordTableViewCell.h"
#import "ZGLTopicDetModel.h"
#import "ZGLHTTPTOOL.h"
#import <MJRefresh.h>

@interface ZGLTopicTypeVC () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *topicTableView;

@property (nonatomic, copy) NSString *urlStr;
@property (nonatomic, assign) NSInteger temNum;
@property (nonatomic, copy) NSString *showDate;


@end

@implementation ZGLTopicTypeVC

- (void)dealloc {
    _topicTableView.dataSource = nil;
    _topicTableView.delegate = nil;
}

- (void)createTableView {
    self.temNum = 1;
    self.topicTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScaleX * 375, self.view.frame.size.height + 98) style:(UITableViewStylePlain)];
    self.topicTableView.delegate = self;
    self.topicTableView.dataSource = self;

    [self.topicTableView registerClass:[ZGLTopicImageTableViewCell class] forCellReuseIdentifier:@"ZGLTopicTypeVC_ZGLTopicImageTableViewCell"];
    [self.topicTableView registerClass:[ZGLTopicWordTableViewCell class] forCellReuseIdentifier:@"ZGLTopicTypeVC_ZGLTopicWordTableViewCell"];
    [self.topicTableView registerClass:[ZGLTopicWordTableViewCell class] forCellReuseIdentifier:@"ZGLTopicTypeVC_ZGLTopicWordTableViewCelll"];
    [self.topicTableView registerClass:[ZGLTopicWordTableViewCell class] forCellReuseIdentifier:@"ZGLTopicTypeVC_ZGLTopicWordTableViewCellll"];
    [self.view addSubview:self.topicTableView];

}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = backColor;
    self.topicTableView.backgroundColor = backColor;
    [self.topicTableView reloadData];
}
- (void)setNavigationBar {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zaihufanhui"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftBarButtonItem:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
}
- (void)leftBarButtonItem:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 上拉刷新
- (void)setHeaderRefresh {
    self.topicTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.topicTableView.header beginRefreshing];
        [self handleData];
        self.temNum = 1;
    }];
}
- (void)handleData {
    self.urlStr = @"http://iphone.myzaker.com/zaker/topic_list_new.php?since_date=";
    self.urlStr = [self.urlStr stringByAppendingString:_dateStr];
    [ZGLHTTPTOOL GETWithURL:self.urlStr withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        NSMutableArray *arr = result[@"data"][@"list"][0][@"topic_list"];
        _topicModelArr = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dict in arr) {
            ZGLTopicDetModel *model1 = [ZGLTopicDetModel modWithDic:dict];
            [_topicModelArr addObject:model1];
        }
        [self.topicTableView.header endRefreshing];
        [self.topicTableView reloadData];
    } withFail:^(id result) {
        
    }];

}

#pragma mark - 下拉加载
- (void)setFooterRefresh {
    self.topicTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.urlStr = @"http://iphone.myzaker.com/zaker/topic_list_new.php?since_date=";
        self.urlStr = [self.urlStr stringByAppendingString:[NSString stringWithFormat:@"%ld",[_dateStr integerValue] - self.temNum]];
        [ZGLHTTPTOOL GETWithURL:self.urlStr withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
            _showDate = result[@"data"][@"list"][0][@"date"];
            NSMutableArray *arr = result[@"data"][@"list"][0][@"topic_list"];
            _total = _topicModelArr.count;
            for (NSDictionary *dict in arr) {
                ZGLTopicDetModel *model1 = [ZGLTopicDetModel modWithDic:dict];
                [_topicModelArr addObject:model1];
            }
            
            [self.topicTableView reloadData];
        } withFail:^(id result) {
            
        }];
        self.temNum++;
        [self.topicTableView.footer endRefreshing];
        
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationBar];
    [self createTableView];
    [self setHeaderRefresh];
    [self setFooterRefresh];
    // Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_topicModelArr.count == 0) {
        [self.topicTableView.footer setFrame:CGRectMake(ScaleX * 87.5, 0, ScaleY * 200, 49)];
        self.topicTableView.footer.backgroundColor = backColor;
        self.topicTableView.footer.layer.cornerRadius = 10;
        self.topicTableView.footer.layer.masksToBounds = YES;
        self.topicTableView.footer.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.topicTableView.footer.layer.borderWidth = 1.0f;
        return 1;
    } else {
        return _topicModelArr.count;
    }
}
- (void)setTopicModelArr:(NSMutableArray *)topicModelArr {
    if (_topicModelArr != topicModelArr) {
        _topicModelArr = topicModelArr;
    }
    [self.topicTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    ZGLTopicDetModel *model =_topicModelArr[section];
    if (model.gallery.count == 0) {
        return 3;
    } else {
        return 4;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZGLTopicDetModel *model =_topicModelArr[indexPath.section];
    if (model.gallery.count == 0) {
        if (0 == indexPath.row){
            ZGLTopicWordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLTopicTypeVC_ZGLTopicWordTableViewCell"];
            cell.wordLabel.text = model.article[0][@"title"];
            cell.wordLabel.textColor = artiColor;
            cell.wordLabel.font = [UIFont systemFontOfSize:15];
            return cell;
        }
        if (1 == indexPath.row){
            ZGLTopicWordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLTopicTypeVC_ZGLTopicWordTableViewCelll"];
            cell.wordLabel.text = model.article[1][@"title"];
            cell.wordLabel.textColor = artiColor;
            cell.wordLabel.font = [UIFont systemFontOfSize:15];
            return cell;
        } else {
            ZGLTopicWordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLTopicTypeVC_ZGLTopicWordTableViewCellll"];
            cell.wordLabel.text = model.entrance[0][@"title"];
            cell.wordLabel.textColor = naviColor;
            cell.wordLabel.font = [UIFont systemFontOfSize:13];
            return cell;
        }

    } else {
        if (0 == indexPath.row) {
            ZGLTopicImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLTopicTypeVC_ZGLTopicImageTableViewCell"];
            cell.topicModel = _topicModelArr[indexPath.section];
            return cell;
        }
        if (1 == indexPath.row){
            ZGLTopicWordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLTopicTypeVC_ZGLTopicWordTableViewCell"];
            cell.wordLabel.text = model.article[0][@"title"];
            cell.wordLabel.textColor = artiColor;
            cell.wordLabel.font = [UIFont systemFontOfSize:15];
            return cell;
        }
        if (2 == indexPath.row){
            ZGLTopicWordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLTopicTypeVC_ZGLTopicWordTableViewCelll"];
            cell.wordLabel.text = model.article[1][@"title"];
            cell.wordLabel.textColor = artiColor;
            cell.wordLabel.font = [UIFont systemFontOfSize:15];
            return cell;
        } else {
            ZGLTopicWordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLTopicTypeVC_ZGLTopicWordTableViewCellll"];
            cell.wordLabel.text = model.entrance[0][@"title"];
            cell.wordLabel.textColor = naviColor;
            cell.wordLabel.font = [UIFont systemFontOfSize:13];
            return cell;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (0 == section || _total == section) {
        return ScaleY * 30;
    } else {
        return CGFLOAT_MIN;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      ZGLTopicDetModel *model =_topicModelArr[indexPath.section];
    if (model.gallery.count == 0) {
        return ScaleY * 60;
    } else {
        if (indexPath.row == 0) {
            return ScaleY * 180;
        } else {
            return ScaleY * 60;
        }
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = backColor;
    if (0 == section || _total == section) {
        [view1 setFrame:CGRectMake(0, 0, ScaleX * 375, ScaleY * 30)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScaleX * 375, ScaleY * 30)];
        label.textAlignment = 1;
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:11];
        if (0 == section) {
            label.text = @"今天";
        } else {
            label.text = _showDate;
        }
        [view1 addSubview:label];
    }
    return view1;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
